package com.example.gulimall.cas.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.gulimall.cas.model.PermissionDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * <p>@description: 权限  </p>
 * <p>@author: JGD </p>
 * <p>@create: 2020/3/12 10:42 </p>
 * <p>@version : 2.0.0
 **/
@Mapper
@Repository
public interface PermissionDAO extends BaseMapper<PermissionDAO> {
    /**
     * 获取角色权限
     * @param userId 用户Id
     * @return List<PermissionDO>
     */
    @Select("SELECT\n" +
            "  p.*\n" +
            "FROM\n" +
            "  tb_user AS u\n" +
            "  LEFT JOIN tb_user_role AS ur\n" +
            "    ON u.id = ur.user_id\n" +
            "  LEFT JOIN tb_role AS r\n" +
            "    ON r.id = ur.role_id\n" +
            "  LEFT JOIN tb_role_permission AS rp\n" +
            "    ON r.id = rp.role_id\n" +
            "  LEFT JOIN tb_permission AS p\n" +
            "    ON p.id = rp.permission_id\n" +
            "WHERE u.id = #{userId}")
    List<PermissionDO> selectPermission(@Param("userId") String userId);
    /**
     * 获取角色权限权限URl
     * @param username 用户名称
     * @return List<PermissionDO>
     */
    @Select("SELECT\n" +
            "  p.url\n" +
            "FROM\n" +
            "  tb_user AS u\n" +
            "  LEFT JOIN tb_user_role AS ur\n" +
            "    ON u.id = ur.user_id\n" +
            "  LEFT JOIN tb_role AS r\n" +
            "    ON r.id = ur.role_id\n" +
            "  LEFT JOIN tb_role_permission AS rp\n" +
            "    ON r.id = rp.role_id\n" +
            "  LEFT JOIN tb_permission AS p\n" +
            "    ON p.id = rp.permission_id\n" +
            "WHERE u.username = #{username}")
    Set<String> selectPermissionUrl(@Param("username") String username);

}
